//
//  HEMineHeadView.m
//  HealthFamily
//
//  Created by 吴欧 on 2020/10/16.
//  Copyright © 2020 honglutec. All rights reserved.
//

#import "HEMineHeadView.h"
#import "HEMySignViewController.h"
#import "HEBusinessCardViewController.h"
#import "HEPersonCenterViewController.h"
#import "HERealNameAuthAlterController.h"

@interface HEMineHeadView ()

@property (nonatomic, strong) UIImageView *qrCode;
@property (nonatomic, strong) UILabel *realLb;
@property (nonatomic, strong) UILabel *healthCodeLb;
@property (nonatomic, strong) UIImageView *userIcon;
@property (nonatomic, strong) UILabel *nicknameLb;

@end

@implementation HEMineHeadView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self initUI];
        
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshUserInfo) name:KNotificationRealAuth object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshUserInfo) name:KNotificationUserInfoChange object:nil];
        
    }
    return self;
}

- (void)initUI {
    
    // 黑色背景
    UIImageView *backImgView = [UIImageView new];
    [backImgView setImage:IMAGE_NAMED(@"my_title_bg")];
    backImgView.contentMode = UIViewContentModeScaleAspectFill;
    backImgView.clipsToBounds = YES;
    [self addSubview:backImgView];
    backImgView.userInteractionEnabled = YES;

    [backImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self).mas_offset(-kTabBarHeight);
        make.left.equalTo(self).mas_offset(0);
        make.right.equalTo(self).mas_offset(0);
        make.height.mas_offset(162);
    }];
    
    // 卡片背景
    UIImageView *cardView = [UIImageView new];
    [cardView setImage:IMAGE_NAMED(@"my_card")];
    cardView.userInteractionEnabled = YES;
    cardView.contentMode = UIViewContentModeScaleAspectFill;
    cardView.layer.cornerRadius = 12;
    cardView.layer.masksToBounds = YES;
    cardView.clipsToBounds = YES;
    [self addSubview:cardView];
    UITapGestureRecognizer *cardTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(toSignClick)];
    [cardView addGestureRecognizer: cardTap];
    
    [cardView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self).mas_offset(25);
        make.left.equalTo(self).mas_offset(15);
        make.right.equalTo(self).mas_offset(-15);
        make.height.mas_offset(160);
    }];

    UserInfo *user = [[UserManager sharedUserManager] getUserInfo];

    // 头像
    UIImageView *userIcon = [UIImageView new];
    [userIcon sd_setImageWithURL:[NSURL URLWithString:user.avatar] placeholderImage:IMAGE_NAMED(@"defualt_icon")];
    userIcon.contentMode = UIViewContentModeScaleAspectFill;
    userIcon.layer.masksToBounds = YES;
    userIcon.layer.cornerRadius = 35;
    [cardView addSubview:userIcon];
    _userIcon = userIcon;
    
    userIcon.userInteractionEnabled = YES;
    userIcon.multipleTouchEnabled = YES;
    UITapGestureRecognizer *headTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(toPersonClick)];
    [userIcon addGestureRecognizer: headTap];

    [userIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(cardView).mas_offset(15);
        make.left.equalTo(cardView).mas_offset(15);
        make.width.mas_offset(70);
        make.height.mas_offset(70);
    }];
    
    // 二维码
    UIImageView *qrCode = [UIImageView new];
    [qrCode setTintColor: [UIColor colorWithHexString:@"#93641E"]];
    qrCode.contentMode = UIViewContentModeScaleAspectFill;
    [cardView addSubview:qrCode];
    _qrCode = qrCode;
    qrCode.userInteractionEnabled = YES;
    UITapGestureRecognizer *codeTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(toCodeClick)];
    [qrCode addGestureRecognizer: codeTap];

    [qrCode mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(userIcon.mas_top);
        make.right.equalTo(cardView.mas_right).mas_offset(-15);
        make.width.mas_offset(58);
        make.height.mas_offset(58);
    }];
    [_qrCode sd_setImageWithURL:[NSURL URLWithString:user.qrcode_url] placeholderImage:IMAGE_NAMED(@"my_qr_code")];

    // 昵称
    UILabel *nicknameLb = [UILabel new];
    [cardView addSubview:nicknameLb];
    nicknameLb.text = [DYTools isBlankString:user.nickname] ? user.username : user.nickname;
    nicknameLb.font = DYFont(21);
    nicknameLb.textColor = [UIColor colorWithHexString:@"#93641E"];
    _nicknameLb = nicknameLb;
    
    [nicknameLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(userIcon.mas_top);
        make.left.equalTo(userIcon.mas_right).mas_offset(10);
    }];
    
    // 实名标记
    UILabel *realLb = [UILabel new];
    [cardView addSubview:realLb];
    realLb.text = user.verifyStatus == 1 ? @"已实名": @"未实名>";
    realLb.font = DYFont(14);
    realLb.textAlignment = NSTextAlignmentCenter;
    realLb.backgroundColor = KWhiteColor;
    realLb.layer.masksToBounds = YES;
    realLb.layer.cornerRadius = 4;
    realLb.textColor = [UIColor colorWithHexString:@"#93641E"];
    _realLb = realLb;
    
    realLb.userInteractionEnabled = YES;
    UITapGestureRecognizer *realTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(toVaildRealUser)];
    [realLb addGestureRecognizer: realTap];
    
    [realLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(nicknameLb);
        make.left.equalTo(nicknameLb.mas_right).mas_offset(12);
        make.width.greaterThanOrEqualTo(@54);
    }];
    
    // 健康码
    UILabel *healthCodeLb = [UILabel new];
    [cardView addSubview:healthCodeLb];
    if (![DYTools isBlankString:user.healthCode]) {
        healthCodeLb.text = [NSString stringWithFormat:@"NO.%@", user.healthCode];
    }
    healthCodeLb.font = DYFont(16);
    healthCodeLb.numberOfLines = 0;
    healthCodeLb.textColor = [UIColor colorWithHexString:@"#93641E"];
    _healthCodeLb = healthCodeLb;
    
    [healthCodeLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(nicknameLb.mas_bottom).mas_offset(14);
        make.left.equalTo(nicknameLb);
        make.right.equalTo(qrCode.mas_right).mas_offset(-10);
    }];
    
    
    // 进度条
    UIProgressView *processView = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleDefault];
    [cardView addSubview:processView];
    processView.trackTintColor = [UIColor colorWithHexString:@"#E6CB9C"];
    processView.progressTintColor = [UIColor colorWithHexString:@"#C29355"];
    [processView setProgress:0 animated:YES];

    [processView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(cardView.mas_bottom).mas_offset(-42);
        make.left.equalTo(cardView.mas_left).mas_offset(15);
        make.right.equalTo(cardView.mas_right).mas_offset(-15);
        make.height.mas_offset(3);
    }];
    
//    // 会员等级
//    UILabel *levelLb = [UILabel new];
//    [cardView addSubview:levelLb];
//    levelLb.text = [NSString stringWithFormat:@"V%d会员等级    成长值%d/%d",0,0,0];
//    levelLb.font = DYBlodFont(16);
//    levelLb.numberOfLines = 0;
//    levelLb.textColor = [UIColor colorWithHexString:@"#93641E"];
//
//    [levelLb mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.equalTo(processView.mas_bottom).mas_offset(10);
//        make.left.equalTo(processView);
//        make.right.equalTo(processView);
//    }];
//
//
//    // 右箭头
//    UIImageView *rightImg = [UIImageView new];
//    [rightImg setImage:IMAGE_NAMED(@"my_go_brown")];
//    rightImg.contentMode = UIViewContentModeScaleAspectFill;
//    [cardView addSubview:rightImg];
//
//    [rightImg mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.centerY.equalTo(levelLb);
//        make.right.equalTo(cardView.mas_right).mas_offset(-10);
//        make.width.mas_offset(20);
//        make.height.mas_offset(24);
//    }];
}

- (void)toSignClick {
//    HEMySignViewController *vc= [HEMySignViewController new];
//    [_vc.navigationController pushViewController:vc animated:YES];
}

- (void)toCodeClick {
    if (![UserManager sharedUserManager].curUserInfo.verifyStatus) {
        [Dialog toastBottom:@"实名后会为你生成二维码哦~"];
        return;
    }
    HEBusinessCardViewController *vc= [HEBusinessCardViewController new];
    [_vc.navigationController pushViewController:vc animated:YES];
}

- (void)toPersonClick {
    HEPersonCenterViewController *vc= [HEPersonCenterViewController new];
    [_vc.navigationController pushViewController:vc animated:YES];
}

- (void)toVaildRealUser {
    if (![[UserManager sharedUserManager] getUserInfo].verifyStatus) {
        HERealNameAuthAlterController *vc = [HERealNameAuthAlterController new];
        [_vc.view addSubview:vc.view];
     }
}

- (void)refreshUserInfo {
    UserInfo *user = [[UserManager sharedUserManager] getUserInfo];
    
    [_userIcon sd_setImageWithURL:[NSURL URLWithString:user.avatar] placeholderImage:IMAGE_NAMED(@"defualt_icon")];
    _nicknameLb.text = [DYTools isBlankString:user.nickname] ? user.username : user.nickname;

    [_qrCode sd_setImageWithURL:[NSURL URLWithString:user.qrcode_url] placeholderImage:IMAGE_NAMED(@"my_qr_code")];
    _realLb.text = user.verifyStatus == 1 ? @"已实名": @"未实名>";
    if (![DYTools isBlankString:user.healthCode]) {
        _healthCodeLb.text = [NSString stringWithFormat:@"NO.%@", user.healthCode];
    } 
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:KNotificationRealAuth object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:KNotificationUserInfoChange object:nil];;
}

@end
